package com.jeesite.modules.dataScreen.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jeesite.modules.dataScreen.dao.IndustryIndexDao;
import com.jeesite.modules.dataScreen.dao.sjtbdb.SjtbDbDao;
import com.jeesite.modules.dataScreen.dao.themedb.ThemeDbDao;
import com.jeesite.modules.dataScreen.entity.Tianbao;
import com.jeesite.modules.dataScreen.web.vo.IndustryIndexVo;
import com.jeesite.modules.dataScreen.web.vo.supervisor.UnReportItemVo;
import com.jeesite.modules.dataScreen.web.vo.supervisor.UnReportVo;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 监督评价模块
 */
@Service
public class SupervisorService {

    @Resource
    private ThemeDbDao themeDbDao;

    @Resource
    private IndustryIndexDao industryIndexDao;

    @Resource
    private SjtbDbDao sjtbDbDao;

    @Resource
    private IndustryIndexService industryIndexService;


    public List<UnReportVo> unReportVo() {
        Collection<UnReportVo> unReportVos = CollUtil.addAll(
                themeDbDao.getUnReport(), industryIndexDao.getUnReport());

        List<UnReportVo> reportVos = new ArrayList<>(unReportVos.stream().collect(
                Collectors.toMap(UnReportVo::getTbdw, Function.identity(), (v1, v2) -> {
            v1.setUnCount(v1.getUnCount() + v2.getUnCount());
            return v1;
        })).values());
        reportVos.sort(Comparator.comparing(UnReportVo::getUnCount).reversed());
        return reportVos;
    }

    public List<UnReportItemVo> unReportList(String tbdwName) {
        List<UnReportItemVo> results = new ArrayList<>();
        results.addAll(themeDbDao.getUnReportList(tbdwName));
        results.addAll(industryIndexDao.getUnReportList(tbdwName));
        results.sort(Comparator.comparing(UnReportItemVo::getTbdw));
        return results;
    }

    public List<JSONObject> credit() {
        List<JSONObject> result = sjtbDbDao.credit();
        return result;
    }

    public List<IndustryIndexVo> administrative() {
//        List<JSONObject> result = sjtbDbDao.administrative();
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("hpl", result.get(0).getString("hpl"));
//        jsonObject.put("cql", result.get(0).getString("cql"));
//        result.remove(0);
//        for (JSONObject object : result) {
//            object.remove("hpl");
//            object.remove("cql");
//        }
//        jsonObject.put("pm", result);
//        return jsonObject;

        String[] strings = new String[]{"群众好评率",
                "行政许可办结率","行政许可受理率","行政许可不予受理率"};
        Map<String, IndustryIndexVo> dataMap = industryIndexService.getUnRealtime(strings);
        return industryIndexService.getList(dataMap,IndustryIndexVo.consumerColor(),strings);

    }
}
